<?php

/**
 * Pareto class
 *
 * Plot Pareto
 *
 * @name Pareto
 * @author DiMoN
 * @copyright Dmitry Pavlov
 */

class Pareto extends Chart implements IChart
{
    const CHART_ID = "Pareto",
    CHART_TYPE = 2,
    CHART_NAME = "Диаграмма Парето";

    /**
     * Pareto constructor
     *
     */
    function __construct()
    {
        $this->isParamsValid();
    }

    /**
     * Gets chart ID specified by the const
     *
     * @return string
     */
    public static function getChartID()
    {
        return self::CHART_ID;
    }

    /**
     * Gets chart Type specified by the const
     *
     * @return int
     */
    public static function getChartType()
    {
        return self::CHART_TYPE;
    }

    /**
     * Gets chart Name specified by the const
     *
     * @return string
     */
    public static function getChartName()
    {
        return self::CHART_NAME;
    }

    /**
     * Checks the validity of the data arrays
     *
     */
    public function isParamsValid()
    {
        if (!isset(parent::$Data2D))
        {
            throw new Exception(self::CHART_ID . ": Data2D array is not set");
        }

        if (!isset(parent::$Labels))
        {
            throw new Exception(self::CHART_ID . ": Labels array is not set");
        }
    }

    /**
     * Plots chart
     *
     * @return string
     */
    public function plot()
    {
        $c = new XYChart(parent::$Width, parent::$Height, parent::$Background);
        $c->setRoundedFrame();

        $textBoxObj = $c->addTitle(parent::$Title, parent::$TitleFont, parent::$TitleFontSize, parent::$ColorTitle);
        $textBoxObj->setBackground(parent::$BackgroundTitle1, parent::$BackgroundTitle2, glassEffect());

        $c->setPlotArea(
        60,
        40,
        parent::$Width  - parent::$Width  / 7.3,
        parent::$Height - parent::$Height / 8 - parent::$LabelLevel * 10,
        $c->linearGradientColor(
        parent::$Width  - parent::$Width  / 7.3,
        parent::$Height - parent::$Height / 1,
        parent::$Width  - parent::$Width  / 7.3,
        parent::$Height - parent::$Height / 8,
        parent::$BackgroundArea1,
        parent::$BackgroundArea2),
        -1,
        parent::$BorderColor,
        parent::$HorizontalLineColor,
        parent::$VerticalLineColor
        );

        $c->xAxis->setTitle(parent::$NameY, parent::$LabelFont);
        $lineLayer = $c->addLineLayer();

        /**
         * Sort data array with labels in descending order
         */
        $dataLabelsArray = array();
        foreach (parent::$Data2D as $key => $dataValue)
        {
            $dataLabelsArray[$key] = $dataValue . "|" . parent::$Labels[$key];
        }
        rsort($dataLabelsArray, SORT_NUMERIC);

        $Data2D = array();
        $LeveledLabels = array();
        foreach ($dataLabelsArray as $key => $value)
        {
            $explodedArray = explode("|", $value, 2);
            $Data2D[] = $explodedArray[0];
            $LeveledLabels[] = $explodedArray[1];
        }
        $LeveledLabels = parent::setLabelsLevel($LeveledLabels);
         
        /**
         * Adding sorted data array
         */
        $lineData = new ArrayMath($Data2D);

        /**
         * Replacing each element of the ArrayMath object
         * by the accumulating total of its previous element (including itself)
         */
        $lineData->acc();

        $scaleFactor = 100 / $lineData->max();
        $lineData->mul2($scaleFactor);

        $dataSetObj = $lineLayer->addDataSet($lineData->result(), 0x000080);
        $dataSetObj->setDataSymbol(DiamondSymbol, 9, 0x9999ff);
        $lineLayer->setLineWidth(2);

        $barLayer = $c->addBarLayer3($Data2D);
        $barLayer->setUseYAxis2();
        $barLayer->setBorderColor(Transparent, softLighting(Right));

        /**
         * Adding added quantity to each bar
         */
        $barLayerObj = $c->addBarLayer3($lineData->result());
        $barLayerObj->setBarShape(CircleShape);

        /**
         * Adding labels for sorted data array
         */
        $textBoxObj = $c->xAxis->setLabels($LeveledLabels);
        $textBoxObj->setFontAngle(parent::$LabelFontAngle);

        $c->yAxis->setLinearScale(0, 105, 0); 
        
        $c->syncYAxis();
        $c->yAxis->setTitle(parent::$ParamName .' (%)', parent::$LabelFont, 10);
        $c->yAxis2->setTitle(parent::$ParamName,  parent::$LabelFont, 10);
        $c->xAxis->setColors(Transparent);
        $c->yAxis->setColors(Transparent);
        $c->yAxis2->setColors(Transparent);
        $c->xAxis->setLabelStyle('', 8);
        $c->yAxis->setLabelStyle('', 8);
        $c->yAxis2->setLabelStyle('', 8);
        $c->yAxis->setLabelFormat("{value}%");
        $c->yAxis2->setLabelFormat("{value|0}");
        $c->syncYAxis(1 / $scaleFactor);

        return parent::saveChart(&$c);
    }
}

?>